import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import AddEditModal from '#cpt/temp/AddEditModal'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const ControlModals 		= $Lazy.state(()=>import('./cpt/control-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '模板名称',		name: 'lab_name' },
		],
		// 提交表单列表
		formData : [
			{ label: '实验号模板',	name: 'tag_id', 		full:true, type: 'select', 	required:true,
				onValue: ({value,formData})=> {
					$http.submit(this,'lis-lab-section-part/getlastcode',{param:{tag_id:value}}).then( ({last_code})=> {
						this.formRef.setValue({'start_code':last_code})
					})
				}
			},
			{ label: '添加方式',		name: 'is_commission', 	full:true, type: 'radio',	value:'1', 	data:[{name:'按开始号和结束号',value:'1'},{name:'按数量',value:'0'}],
				onValue: ({value,formData})=> {
					$fn.hideRows(formData,'end_code', !Boolean(+value))
					$fn.hideRows(formData,'num', Boolean(+value))
					this.setState({formData})
				}
			},
			{ label: '开始号',		name: 'start_code', 	full:true, type: 'number', 	required:true },
			{ label: '结束号',		name: 'end_code', 		full:true, type: 'number', 	required:true },
			{ label: '数量',		name: 'num', 			full:true, type: 'number', 	required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
		$fn.fresh.on.call(this, this.fetch)
	}
	
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'tag_id', ()=>{
			$cache.then( async f => {
				// 实验号模板
				await $fn.setRowc(formData, 'tag_id',  async () =>  f.getCache({ api: 'num_template'}))
				//
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'lis-lab-section-part/index',{isIndex:true})
	
	// 表单
	cols = [
		{ title: '实验号模板', 	dataIndex: 'lab_name', 	width:150 },
		{ title: '实验段', 		dataIndex: 'section', 		width:200 },
		{ title: '数量', 		dataIndex: 'num', 	width:150 },
		{ title: '创建时间', 	dataIndex: 'created_at', 	...$fn.table.time },
		TableControls.controls.call(this, { width: 180 })
	]
	
	// 表格操作按钮
	table_controls = (text, rows) => {
		return [
			TableControls.click.call(this, '查看', ()=> this.modals.open(rows) ),
			this.onDel(rows.uuid),				// 删除
		]
	}
	
	// 操作按钮
	controls = () => [
		// 添加
		Controls.add.call(this,{click: () => {
			const { formData } = this.state
			this.getModalSelect()
			// 打开时初始化数据 2021-9-9 2:42:26 ?F10: PM? --伍智睿
			this.formRef.setValue({'is_commission':'1'})
			$fn.hideRows(formData,'end_code', false)
			$fn.hideRows(formData,'num', true)
			this.setState({formData})
		}}),	
	]
	
	// 删除
	onDel = uuid => Delete.call(this, 'lis-lab-section-part/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='实验号分段管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{ isIndex:true }) }
					{/* 添加、编辑 */}
					{ AddEditModal.call(this,{ api: 'lis-lab-section-part/add' }) }
				</Page>
				{/* 查看 */}
				<ControlModals onRef={ref=>this.modals=ref} />
			</>
		)
	}
}

export default Index